package cn.smxy.admin.controller;


import cn.smxy.client.CategoryClient;
import cn.smxy.client.CollegeClient;
import cn.smxy.client.PlantClient;
import cn.smxy.param.FamilyFuzzyParam;
import cn.smxy.param.PageParam;
import cn.smxy.pojo.*;
import cn.smxy.util.R;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Controller
@RequestMapping
public class HtmlJumpController {

    @Autowired
    private CategoryClient categoryClient;
    @Autowired
    private PlantClient plantClient;
    @Autowired
    private CollegeClient collegeClient;

    /**
     * 设计欢迎页面跳转controller
     *
     * @return login 登录页面
     */
    @GetMapping({"/", "index.html", "index"})
    public String index() {
        log.info("HtmlJumpController.welcome 跳转登录页面!");
        return "login";
    }

    @GetMapping("/welcome")
    public String welcome() {
        log.info("HtmlJumpController.welcome业务结束，结果为:{}","");
        return "welcome";
    }


    /**
     * 登录成功跳转到index页面
     *
     * @return
     */
    @GetMapping("/home")
    public String home() {
        log.info("HtmlJumpController.home登录成功,跳转程序首页!index页面!");
        return "index";
    }

    /**
     * 跳转用户管理页面
     */
    @GetMapping("/user")
    public String user() {
        log.info("HtmlJumpController.user,跳转用户管理!user页面!");
        return "user/user";
    }

    /**
     * 跳转管理员管理页面
     */
    @GetMapping("/admin")
    public String admin() {
        log.info("HtmlJumpController.user,跳转用户管理!user页面!");
        return "user/admin";
    }

    /**
     * 打开编辑用户页面
     */
    @GetMapping("/user/update/html")
    public String userUpdateHtml() {
        log.info("HtmlJumpController.userUpdateHtml业务结束，结果:{}");
        return "user/edit";
    }

    /**
     * 打开添加用户页面
     */
    @GetMapping("/user/save/html")
    public String userSaveHtml() {
        log.info("HtmlJumpController.userSaveHtml业务结束，结果:{}");
        return "user/add";
    }

    /**
     * 打开编辑种群页面
     */
    @GetMapping("/category/update/html")
    public String categoryUpdateHtml() {
        log.info("HtmlJumpController.categoryUpdateHtml业务结束，结果:{}");
        return "category/edit";
    }

    /**
     * 添加种群页面
     *
     * @return
     */
    @GetMapping("/category/save/html")
    public String categorySaveHtml() {
        log.info("HtmlJumpController.categorySaveHtml结束，结果:{}");
        return "category/add";
    }

    /**
     * 跳转校园认证页面
     */
    @GetMapping("/campus")
    public String campus(Model model) {
        R r = collegeClient.list();
        ObjectMapper objectMapper = new ObjectMapper();
        List<College> collegeList = objectMapper.convertValue(r.getData(), new TypeReference<List<College>>() {
        });
        model.addAttribute("collegeList",collegeList);
        log.info("HtmlJumpController.campus业务结束，结果为:{}", "");
        return "user/campus";
    }

    @GetMapping("/campus/save/html")
    public String campusSave(Model model) {
        R r = collegeClient.list();
        ObjectMapper objectMapper = new ObjectMapper();
        List<College> collegeList = objectMapper.convertValue(r.getData(), new TypeReference<List<College>>() {
        });
        model.addAttribute("collegeList",collegeList);
        log.info("HtmlJumpController.campusSave业务结束，结果为:{}","");
        return "user/campusAdd";
    }

    /**
     * 跳转用户反馈页面
     */
    @GetMapping("/feedback")
    public String feedback() {
        log.info("HtmlJumpController.feedback业务结束，结果为:{}", "");
        return "user/feedback";
    }

    /**
     * 跳转学校页面
     */
    @GetMapping("/college")
    public String college() {
        log.info("HtmlJumpController.college业务结束，结果为:{}", "");
        return "college/college";
    }

    /**
     * 跳转添加学校页面
     */
    @GetMapping("/college/save/html")
    public String collegeSaveHtml() {
        log.info("HtmlJumpController.collegeSaveHtml业务结束，结果为:{}","");
        return "college/add";
    }

    /**
     * 跳转修改学校页面
     */
    @GetMapping("/college/update/html")
    public String collegeUpdateHtml() {
        log.info("HtmlJumpController.collegeUpdateHtml业务结束，结果为:{}","");
        return "college/edit";
    }

    /**
     * 跳转校园认证作品页面
     */
    @GetMapping("/collegePlant")
    public String collegePlant(Model model) {
        R r = collegeClient.list();
        ObjectMapper objectMapper = new ObjectMapper();
        List<College> collegeList = objectMapper.convertValue(r.getData(), new TypeReference<List<College>>() {
        });
        model.addAttribute("collegeList",collegeList);
        log.info("HtmlJumpController.collegePlant业务结束，结果为:{}", "");
        return "college/collegePlant";
    }

    /**
     * 跳转种群管理页面
     */
    @GetMapping("/category")
    public String category() {
        log.info("HtmlJumpController.category,跳转用户管理!category页面!");
        return "category/category";
    }

    /**
     * 跳转科属管理页面
     */
    @GetMapping("/family")
    public String family(Model model) {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        log.info("HtmlJumpController.family业务结束，结果为:{}", "");
        return "category/family";
    }

    /**
     * 添加科名页面
     */
    @GetMapping("/family/saveOne/html")
    public String familyAddOne(Model model) {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        log.info("HtmlJumpController.familyAddOne业务结束，结果为:{}", "");
        return "category/familyAddOne";
    }

    /**
     * 添加属名页面
     */
    @GetMapping("/family/saveTwo/html")
    public String familyAddTwo(Model model) {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        log.info("HtmlJumpController.familyAddTwo业务结束，结果为:{}", "");
        return "category/familyAddTwo";
    }

    /**
     * 跳转专题管理页面
     */
    @GetMapping("/subjectType")
    public String subjectType() {
        log.info("HtmlJumpController.subjectType业务结束，结果为:{}", "");
        return "category/subjectType";
    }

    /**
     * 跳转植物管理页面
     */
    @GetMapping("/plant")
    public String plant(Model model) {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        log.info("HtmlJumpController.plant业务结束，结果为:{}", "");
        return "works/plant";
    }

    /**
     * 跳转添加植物页面
     */
    @GetMapping("/plant/save/html")
    public String plantSaveHtml(Model model) {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        log.info("HtmlJumpController.plantSaveHtml业务结束，结果为:{}", "");
        return "works/add";
    }

    /**
     * 跳转修改植物页面
     */
    @GetMapping("/plant/update/html")
    public String plantUpdateHtml(Model model, HttpServletRequest request) throws JsonProcessingException {
        List<Category> list = categoryClient.list();
        model.addAttribute("clist", list);
        // TODO: 读取科名列表 属名列表
        Long categoryId = Long.valueOf(request.getParameter("categoryId"));
        Long familyId = -1L;
        String value = request.getParameter("familyId");
        if(value != null && !value.equals("null")) {
            familyId = Long.valueOf(value);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        R r1 = categoryClient.show(categoryId);
        List<PlantFamily> plantFamilies = objectMapper.convertValue(r1.getData(), new TypeReference<List<PlantFamily>>() {
        });
        R r2 = categoryClient.genusShow(familyId);
        List<PlantFamily> plantGenusList = objectMapper.convertValue(r2.getData(), new TypeReference<List<PlantFamily>>() {
        });
        if(familyId != -1L) {
            FamilyFuzzyParam familyFuzzyParam = new FamilyFuzzyParam();
            familyFuzzyParam.setPageSize(2000);
            R r3 = categoryClient.fuzzy(familyFuzzyParam);
            List<PlantFamily> all = objectMapper.convertValue(r3.getData(), new TypeReference<List<PlantFamily>>() {
            });
            if (plantGenusList == null || plantGenusList.size() == 0) {
                for (PlantFamily plantFamily : all) {
                    if (plantFamily.getFamilyId().equals(familyId)) {
                        model.addAttribute("parentId", plantFamily.getParentId());
                        R r4 = categoryClient.genusShow(plantFamily.getParentId());
                        plantGenusList = objectMapper.convertValue(r4.getData(), new TypeReference<List<PlantFamily>>() {
                        });
                    }
                }
            }
        }
        model.addAttribute("flist", plantFamilies);
        model.addAttribute("glist", plantGenusList);
        log.info("HtmlJumpController.plantEditHtml业务结束，结果为:{}", "");
        return "works/edit";
    }

    /**
     * 跳转图片管理页面
     */
    @GetMapping("/picture")
    public String picture(Model model,HttpServletRequest request) {
        String search = request.getParameter("search");
        if(search == null) {
            search ="";
        }
        List<PlantPicture> plantPictures = plantClient.pictureAll(search);
        model.addAttribute("plist",plantPictures);
        log.info("HtmlJumpController.picture业务结束，结果为:{}", "");
        return "works/picture";
    }

    /**
     * 跳转专题管理页面
     */
    @GetMapping("/subject")
    public String subject() {
        log.info("HtmlJumpController.subject业务结束，结果为:{}", "");
        return "works/subject";
    }

    /**
     * 跳转接口日志页面
     */
    @GetMapping("/log_api")
    public String logApi() {
        log.info("HtmlJumpController.logApi业务结束，结果为:{}","");
        return "log/api";
    }
}

